package com.oedtech.esl.redis;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;


/**
 * @Author QiuLongYu
 * @Date 2019/3/14
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    public void putList(String listKey,String jsonObject){
        redisTemplate.opsForList().leftPush(listKey,jsonObject);
    }

    public List<String> getList(String listKey){
        List<String>list=redisTemplate.opsForList().range(listKey,0,-1);
        redisTemplate.opsForList().trim(listKey,-1,0);
        return list;
    }

    public void putHash(String hash,String key,Object obj){
        redisTemplate.opsForHash().put(hash,key, JSONObject.toJSONString(obj));
    }

    public void putHashAll(String hash, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(hash,map);
    }


    public Map<Object,Object> getHash(String hash){
        Map<Object,Object>map=redisTemplate.opsForHash().entries(hash);
        if(map.isEmpty())return null;
        redisTemplate.delete(hash);
        return map;
    }
    public void removeHash(String hash){
        redisTemplate.opsForHash().delete(hash);
    }
}
